package org.nidy.erp.component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.nidy.erp.utils.WeChatBotSender;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Component
public class ScheduledTask {
    private static final String salesBOTUrl="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=9ddd6eec-0b44-4dcf-b2f7-900bca5e0181";
    private static final String salesSendMessage="请及时发送日报至本群！\n@所有人";
    //配置定时任务，定时执行方法
    @Scheduled(cron = "0 0 20,21 * * ?") // 每天21:00执行
    public void sendSalesDailyReport() throws JsonProcessingException {
        // 构建动态变量
        String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        // 构建消息体（Markdown格式）
        Map<String, Object> markdownMessage = new HashMap<>();
        Map<String, String> markdownContent = new HashMap<>();
        markdownContent.put("content", "**【温馨提示】**\n>日期：" + currentTime + "\n>详情：\n" + salesSendMessage);
        markdownMessage.put("msgtype", "markdown");
        markdownMessage.put("markdown", markdownContent);
        // 转换为JSON字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonBody = objectMapper.writeValueAsString(markdownMessage);
        //调用发送消息的方法并传递参数
        WeChatBotSender.sendMessage(salesBOTUrl, jsonBody);
    }
}
